package com.aaa.controller;

import com.aaa.entity.T_Online;
import com.aaa.service.T_OnlineService;
import com.aaa.util.ExcelUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.*;

/**
 * @Author: TT
 * @Date: 2019/3/28 9:14
 * @Version 1.0
 */
@Controller

@RequestMapping("/pyOnline/")
public class T_OnlineController {
 private List<T_Online> field=null;
@Autowired
private T_OnlineService service;
    @RequestMapping("toList")
    public String tolist(){
    return "statistics/PyOnline";
}

@SuppressWarnings("all")
@RequestMapping("getInfo")
@ResponseBody
    public Map list(@RequestBody Map map){
    List data=service.getInfo(map);
    Integer total=service.getCount(map);
    Map map1=new HashMap();
    map1.put("data",data);
    map1.put("total",total);
    return map1;
    }

    @SuppressWarnings("all")
    @RequestMapping("data")
    @ResponseBody
    public int excel(@RequestBody Map map){
        field =new ArrayList<T_Online>();
        List<Map> list= (List) map.get("xinxi");
        for (int i=0;i<list.size();i++){
            T_Online pyOnline=new T_Online();
            pyOnline.setName((String) list.get(i).get("name"));
            pyOnline.setSHtime((String) list.get(i).get("SHtime"));
            pyOnline.setAppscore((Integer) list.get(i).get("appscore"));
            field.add(pyOnline);
            System.out.println(list.get(i));
        }
        return 1;
    }
    @SuppressWarnings("all")
    @RequestMapping("export")
    @ResponseBody
    public void export(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取数据
        List<T_Online> list = field;
        //excel标题
        String[] title = {"单位名称", "分数", "评议时间"};

        //excel文件名
        String fileName = "在线评议信息表" + System.currentTimeMillis() + ".xls";

        //sheet名
        String sheetName = "评议信息表";
        String[][] content = new String[list.size()][];
        for (int i = 0; i < list.size(); i++) {
            content[i] = new String[title.length];
            T_Online obj = list.get(i);
            content[i][0] = obj.getName().toString();
            content[i][1] = String.valueOf(obj.getAppscore());
            content[i][2] = obj.getSHtime().toString();
        }


        //创建HSSFWorkbook
        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);

        //响应到客户端
        try {
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //发送响应流方法
    @SuppressWarnings("all")
    public void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(),"ISO8859-1");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
